package com.xywl.escort.base;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.xywl.escort.NmApplication;
import com.xywl.escort.R;

import qiu.niorgai.StatusBarCompat;

public class BaseActivity extends RxAppCompatActivity {

    protected Button sBackBtn;

    protected View mViewLine;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        NmApplication.getInstance().addActivity(this);

        StatusBarCompat.translucentStatusBar(this, true);
    }

    /*******************************************************
     * 说明：快速设置标题名，标题组件的的id必须为text_title;
     ********************************************************/
    protected void setTitleText(CharSequence text) {
        TextView textView = findViewById(R.id.tv_title);
        if (textView != null) {
            textView.setText(text);
        }
    }

    /*******************************************************
     * 说明：快速设置标题名，标题组件的的id必须为text_title;
     * @param resId
     ********************************************************/
    protected void setTitleText(int resId) {
        TextView textView = findViewById(R.id.tv_title);
        if (textView != null) {
            textView.setText(resId);
        }
    }

    protected void showBackButton() {
        sBackBtn = findViewById(R.id.btn_back);
        if (sBackBtn != null) {
            sBackBtn.setVisibility(View.VISIBLE);
        }
    }

    protected void showTitleLine() {
        mViewLine = findViewById(R.id.view_title_line);
        if (null != mViewLine) {
            mViewLine.setVisibility(View.VISIBLE);
        }
    }

    public void doBack(View v) {
        finish();
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            createConfigurationContext(config);
        } else {
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
        return res;
    }

    @Override
    protected void onDestroy() {
        NmApplication.getInstance().removeActivity(this);
        super.onDestroy();
    }
}
